package es.mgil.whoWantsToBeAMillonaire.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import es.mgil.whoWantsToBeAMillonaire.R;
import es.mgil.whoWantsToBeAMillonaire.activities.Play;
import es.mgil.whoWantsToBeAMillonaire.util.MusicManager;
import es.mgil.whoWantsToBeAMillonaire.util.PreferencesKeys;

public class ShouldContinueDialog extends DialogFragment {
	public static final String CONTINUE_DIALOG_ID = "continue_dialog_id";

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final Play play = (Play) getActivity();

		AlertDialog.Builder builder = new AlertDialog.Builder(play);
		builder.setMessage(R.string.play_dialog_continue);
		builder.setPositiveButton(R.string.play_dialog_yes,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (MusicManager.isMusicEnabled(play.preferences)
								&& play.mmWinQuestion.isPlaying()) {
							// if music is enabled, and music is still playing,
							// stop it
							play.mmWinQuestion.stopPlayer();
						}

						// reset some preferences before showing next question
						play.editor
								.putInt(PreferencesKeys.PREFERENCES_QUESTION_NUMBER,
										play.preferences
												.getInt(PreferencesKeys.PREFERENCES_QUESTION_NUMBER,
														1) + 1);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_PHONE_USED);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_PERCENT_USED);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_AUDIENCE_USED);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_SELECTED_ANSWER);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_PROGRESS_BAR_PROGRESS);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_PROGRESS_STATE);
						play.editor
								.remove(PreferencesKeys.PREFERENCES_QUESTION_MUSIC_PLAYING);
						play.editor.commit();

						// set runnable state to NO_STATE
						play.runnable.noState();

						// reset answers state (delete them from preferences)
						play.resetAnswersState();

						// prepare next question
						play.prepareNextQuestion();
					}
				});
		builder.setNegativeButton(R.string.play_dialog_no,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (MusicManager.isMusicEnabled(play.preferences)
								&& play.mmWinQuestion.isPlaying()) {
							// if music is enabled, and music is still playing,
							// stop it
							play.mmWinQuestion.stopPlayer();
						}

						// get the won score for the current question
						int score = play
								.getAwards()
								.get(play.preferences
										.getInt(PreferencesKeys.PREFERENCES_QUESTION_NUMBER,
												1));

						// save the score into preferences
						play.editor.putInt(
								PreferencesKeys.PREFERENCES_FINAL_SCORE, score);
						play.editor.commit();

						// show finish dialog
						FinishDialog finishDialog = new FinishDialog();
						finishDialog.show(play.getSupportFragmentManager(),
								FinishDialog.FINISH_DIALOG_ID);
					}
				});

		// can't press back button while showing this dialog
		setCancelable(false);
		return builder.create();
	}
}